package com.vcc.data.mapper;

import com.mybatisflex.core.BaseMapper;
import com.vcc.data.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Optional;

@Mapper
public interface UserMapper extends BaseMapper<User> {


    @Select("select a.*,dealer.account as dealerAccount from vcc_user a left join vcc_user dealer on a.be_long_to_dealer = dealer.id where a.id = #{id}")
    Optional<User> findById(
            @Param("id") Long id
    );


    @Select("select a.*,dealer.account as dealerAccount from vcc_user a left join vcc_user dealer on a.be_long_to_dealer = dealer.id where a.account = #{account} and a.password = #{password}")
    Optional<User> findByAccountAndPassword(
            @Param("account") String account,
            @Param("password") String password
    );

    @Select("select account from vcc_user where id = #{id} ")
    String findAccountById(
            @Param("id") Long id
    );

    @Select("select id from vcc_user")
    List<Long> findAllId();

    @Select("select id from vcc_user where parent_id = #{beLongToDealer}")
    List<Long> findIdByBeLongToDealer(@Param("beLongToDealer") Long boLongToDealer);

    @Select("select account from vcc_user vu right join vcc_user_role vur on vur.user_id = vu.id left join vcc_role vr on vur.role_id = vr.id where vr.code = 2")
    List<String> findAllDealerAccount();

    @Select("select vu.* from vcc_user vu right join vcc_user_role vur on vur.user_id = vu.id left join vcc_role vr on vur.role_id = vr.id where vr.code = 2")
    List<User> findAllDealer();

    @Select("select id from vcc_user where account = #{account}")
    Long findIdByAccount(@Param("account") String account);


    @Select("select parent_id from vcc_user where account = #{account}")
    Long findParentIdByAccount(@Param("account") String account);

    @Select("select parent_id from vcc_user where id = #{id}")
    Long findParentIdById(@Param("id") Long id);

    @Select("select be_long_to_dealer from vcc_user where id = #{id}")
    Long getBeLongToDealerById(@Param("id") Long id);
}
